package com.zlq.javaWeb3.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

@WebServlet(urlPatterns = "/fileController")
@MultipartConfig(
         location = "D:\\tup"
)
public class FileController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        Part myfile = req.getPart("myfile");
//        myfile.write("a.png");
        if (myfile != null){
            //获取上传文件的内容类型
            String contentType = myfile.getContentType();
            System.out.println(contentType);
            //获取content-disposition头信息
            String position = myfile.getHeader("content-disposition");
            System.out.println(position);
            int i = position.indexOf("filename=");
            String sub = position.substring(i + 10);
//            sub = sub.substring(sub.lastIndexOf(File.separator)+1);
            sub = sub.substring(0, sub.length() - 1);
            myfile.write(sub);
        }
//        String submittedFileName = myfile.getSubmittedFileName();
//        myfile.write(submittedFileName);

    }
}
